package simple.framework.util.dbc;

import simple.framework.util.*;
import java.sql.*;

public class mysql implements dbc
{
  private int rState = 0;

  private String connStr = "";

  private String eMessage = "";

  public void Execute(String argString)
  {
    String tString = argString;
    if (tString != null)
    {
      try
      {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection tConnection = DriverManager.getConnection(connStr);
        Statement tStatement = tConnection.createStatement();
        tStatement.executeUpdate(tString);
        tStatement.close();
        tConnection.close();
      }
      catch(Exception e)
      {
        eMessage = e.getMessage();
      }
    }
  }

  public void setConnStr(String argString)
  {
    String tString = argString;
    connStr = tString;
  }

  public int Executes(String argString)
  {
    int tCount = -1;
    String tString = argString;
    if (tString != null)
    {
      try
      {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection tConnection = DriverManager.getConnection(connStr);
        Statement tStatement = tConnection.createStatement();
        tCount = tStatement.executeUpdate(tString);
        tStatement.close();
        tConnection.close();
      }
      catch(Exception e)
      {
        tCount = -101;
        eMessage = e.getMessage();
      }
    }
    return tCount;
  }

  public int getRState()
  {
    return rState;
  }

  public Object[] getDataAry(String argString)
  {
    rState = 0;
    String tString = argString;
    Object[] tArys = null;
    if (tString != null)
    {
      try
      {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection tConnection = DriverManager.getConnection(connStr);
        Statement tStatement = tConnection.createStatement();
        ResultSet tResultSet = tStatement.executeQuery(tString);
        ResultSetMetaData tResultSetMetaData = tResultSet.getMetaData();
        int tColumnCount = tResultSetMetaData.getColumnCount(); 
        while(tResultSet.next())
        {
          Object[][] tAry = new Object[tColumnCount][2];
          for (int ti = 0; ti < tColumnCount; ti ++)
          {
            tAry[ti][0] = (Object)tResultSetMetaData.getColumnName(ti + 1);
            tAry[ti][1] = (Object)tResultSet.getObject(ti + 1);
          }
          tArys = cls.mergeAry(tArys, tAry);
        }
        tResultSet.close();
        tStatement.close();
        tConnection.close();
      }
      catch(Exception e)
      {
        rState = 1;
        tArys = null;
        eMessage = e.getMessage();
      }
    }
    return tArys;
  }

  public Object getValue(Object[][] argAry, int argIndex)
  {
    Object tobject;
    Object[][] tAry = argAry;
    int tIndex = argIndex;
    tobject = tAry[tIndex][1];
    return tobject;
  }

  public Object getValue(Object[][] argAry, String argString)
  {
    Object tobject;
    Object[][] tAry = argAry;
    String tString = argString;
    tobject = cls.getAryValue(tAry, tString);
    return tobject;
  }

  public String getEMessage()
  {
    return eMessage;
  }
}